const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter

const user_id = wx.getStorageSync('user_id')
const jg_stu_id = wx.getStorageSync('jg_stu_id')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:true,
    ti_bookmarks:[],
    icon_url:oss_domain + 'static/picture/app_icon/',
    windowWidth:wx.getSystemInfoSync().windowWidth,
  },

  edit_ti(){
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/doTi/edit_ti/edit_ti?ti_info=' + encodeURIComponent(JSON.stringify(this.data.ti_info))
    })
  },


  // 前往首页
  go_index(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },


  // 收藏题
  add_bookmarkTi(e){
    if(!jg_stu_id){
      wx.showModal({
        content: '在训学员方可收藏习题！',
        title: '提示',
      })
      return
    }
    let ti_id = this.data.ti_info.ti_id
    wx.request({
      url: domainName + '/add_bookmarkTi',
      data: {
        ti_id:ti_id,
        jg_stu_id:jg_stu_id,
      },...parameter,
      success: res => {
        getApp().get_vibration()
        this.setData({
          ti_bookmarks:res.data.lis,
        })
      },
    })
  },

  // 初始化加载题
  page_init(jg_stu_id,user_id, ti_id){
    wx.showLoading({
      title: '网络连接中',
    })
    wx.request({
      url: domainName + '/show_ti_init',
      data: {
        jg_stu_id,  // 若没有【jg_stu_id】，那么可查看习题，但不能添加收藏
        user_id,
        ti_id,
      },...parameter,
      success: res => {
        wx.hideLoading()
        this.setData({
          ti_info:res.data.ti_info,
          right_limit:res.data.right_limit,
          ti_bookmarks:res.data.ti_bookmarks,
          loading:false,
        })
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options',options)
    this.ti_id = options.ti_id
    this.page_init(jg_stu_id, user_id, this.ti_id)
    wx.setNavigationBarTitle({
      title: '单习题',
    })
  },

  onReady(){
    this.setData({
      windowHeight:wx.getSystemInfoSync().windowHeight,
      mask_height:wx.getSystemInfoSync().windowHeight * 0.8,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    getApp().get_vibration()
    return {
      // title: this.data.ti_list[this.data.current_list_index].question,
      path: '/pages/doTi/show_ti/show_ti?ti_id=' + this.ti_id
    }
  },
})